<?php

namespace App\Imports;

use App\Events\ImportPersonEvent;
use App\Helpers\ResponseData;
use App\Models\Employee;
use App\Models\Important\ImportantTag;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithValidation;
use App\Models\Important\ImportantPerson;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class ImportantPersonImport implements ToCollection,WithChunkReading,WithBatchInserts,WithValidation,WithStartRow
{
    use ResponseData;
    protected $start = 4;//第几行开始导入
    protected $size = 100;//分块大小
    protected $chunk = 0;//第几块
    public function startRow(): int
    {
        return 2;
    }
    /**
     * @return int
     */
    public function chunkSize(): int
    {
        return $this->size;
    }
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        DB::transaction(function () use($rows){

            foreach ($rows as $key => $row) {
                if(!preg_match("/^1\d{10}$/", $row[3])){
//                    $line = $key + $this->size * ($this->chunk - 1) + $this->start;
                    cache(['import_error'=>'第' . $key . "行，手机号错误"],10);
                }
                $data = [
                    'record_time'=>now(),
                    'data_source'=>$row[0],
                    'person_name'=>$row[1],
                    'id_no'=>$row[2],
                    'cellphone'=>$row[3],
                    'important_tag'=>$row[4],
                ];
                $tag = [
                    'tag_name'=>$row[4]
                ];
                $tag = ImportantTag::query()->firstOrCreate($tag,$tag);
                $user = ImportantPerson::query()->where('id_no',$row[2])->first();
                if($user){
                    $tag = $user->important_tag;
                    if(!strstr($tag,$row[4])){
                        $user->important_tag = $user->important_tag.','.$row[4];
                        $user->save();
                    }
                }else{
                    ImportantPerson::query()->create($data);
                }
                $employee = Employee::query()->select('id','important_tag')->where('em_id_no',$row[2])->first();
                if($employee){
                    $tag = $employee->important_tag;
                    if($tag&&strstr($tag,$row[4])){
                        //return null;
                    }else{
                        if(!$employee->important_tag){
                            $employee->important_tag = $row[4];
                        }else{
                            $employee->important_tag = $employee->important_tag.','.$row[4];
                        }
                        $employee->save();
                    }
                }
            }
            cache(['import_num'=>$key],10);
        });
    }
    public function batchSize(): int
    {
        return 1000;
    }

    public function rules(): array
    {
        return [
            '4'=>function($attribute, $value, $onFailure) {
               if(!preg_match("/^1[34578]\d{9}$/", $value)){
                   return response()->json('手机号不匹配');
               }
//                if ($value !== 'Patrick Brouwers') {
//                    $onFailure('Name is not Patrick Brouwers');
//                }
            }

        ];
    }
}
